/*
 * Reports.java
 *
 * Created on September 27, 2008, 3:37 AM
 */

package rrs;
import java.util.*;
import java.lang.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;

/**
 *
 * @author  Paul Molenda
 */
public class Reports extends javax.swing.JFrame 
{
    /** Creates new form Reports */
    public Reports() {
        initComponents();        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BtnRunReport = new javax.swing.JButton();
        lblSelectDate = new javax.swing.JLabel();
        CbxToday = new java.awt.Checkbox();
        lblOK = new javax.swing.JLabel();
        ComboxMonth = new javax.swing.JComboBox();
        ComboxDay = new javax.swing.JComboBox();
        ComboxYear = new javax.swing.JComboBox();
        lblMonth = new javax.swing.JLabel();
        lblDay = new javax.swing.JLabel();
        lblYear = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TAReport = new javax.swing.JTextArea();
        lblMessage = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        BtnRunReport.setText("Run Report");
        BtnRunReport.setName("BtnRunReport"); // NOI18N
        BtnRunReport.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BtnRunReportMouseClicked(evt);
            }
        });

        lblSelectDate.setFont(new java.awt.Font("Dialog", 0, 12));
        lblSelectDate.setText("Select Report Date:");
        lblSelectDate.setName("lblSelectDate"); // NOI18N

        CbxToday.setLabel("Run Todays Report Only");
        CbxToday.setName("CbxTodayReport"); // NOI18N
        CbxToday.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                CbxTodayItemStateChanged(evt);
            }
        });

        lblOK.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblOK.setText("OR");
        lblOK.setName("lblOK"); // NOI18N

        ComboxMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));
        ComboxMonth.setName("ComboxMonth"); // NOI18N
        ComboxMonth.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ComboxMonthMouseClicked(evt);
            }
        });
        ComboxMonth.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ComboxMonthItemStateChanged(evt);
            }
        });

        ComboxDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        ComboxDay.setName("ComboxDay"); // NOI18N

        ComboxYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2008" }));
        ComboxYear.setName("ComboxYear"); // NOI18N

        lblMonth.setText("Month:");
        lblMonth.setName("lblMonth"); // NOI18N

        lblDay.setText("Day:");
        lblDay.setName("lblDay"); // NOI18N

        lblYear.setText("Year:");
        lblYear.setName("lblYear"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        TAReport.setColumns(20);
        TAReport.setEditable(false);
        TAReport.setRows(10);
        TAReport.setName("TAReport"); // NOI18N
        jScrollPane1.setViewportView(TAReport);

        lblMessage.setFont(new java.awt.Font("Tahoma", 3, 12));
        lblMessage.setName("lblMessage"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(lblMessage))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(14, 14, 14)
                                .addComponent(lblSelectDate))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(48, 48, 48)
                                .addComponent(lblOK))
                            .addComponent(CbxToday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblMonth)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblDay)
                                        .addGap(14, 14, 14)
                                        .addComponent(lblYear))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(ComboxMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ComboxDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ComboxYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(160, 160, 160)
                                .addComponent(BtnRunReport, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(CbxToday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblOK)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblMonth)
                                .addComponent(lblYear)
                                .addComponent(lblDay))))
                    .addComponent(BtnRunReport, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSelectDate)
                    .addComponent(ComboxMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ComboxDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ComboxYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 299, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMessage)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  //Unable to move brackets for autogenerated event declarations
private void CbxTodayItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_CbxTodayItemStateChanged
if (evt.getStateChange()==1)
{
    ComboxMonth.setEnabled(false);
    ComboxDay.setEnabled(false);
    ComboxYear.setEnabled(false);  
}
if (evt.getStateChange()==2)
{
    ComboxMonth.setEnabled(true);
    ComboxDay.setEnabled(true);
    ComboxYear.setEnabled(true);
}
}//GEN-LAST:event_CbxTodayItemStateChanged

//Unable to move brackets for autogenerated event declarations
private void ComboxMonthItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ComboxMonthItemStateChanged

    
}//GEN-LAST:event_ComboxMonthItemStateChanged

//Unable to move brackets for autogenerated event declarations
private void ComboxMonthMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ComboxMonthMouseClicked
CbxToday.setState(false);
CbxToday.setEnabled (false);
}//GEN-LAST:event_ComboxMonthMouseClicked
/*
 Run the report
 */
//Unable to move brackets for autogenerated event declarations
private void BtnRunReportMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BtnRunReportMouseClicked
Date currentDate = new Date();
   
   //clear the previous report
     TAReport.selectAll();
     TAReport.setText("");
     try
       {
          if (CbxToday.getState())
             {
                reportDate = currentDate; 
             }
         else
             {
                 reportDate = createDate(ComboxYear.getSelectedItem().toString(),ComboxMonth.getSelectedItem().toString(),ComboxDay.getSelectedItem().toString());
            }
        
          //is it a future date
        if (!(reportDate.after(currentDate)))    
            {
      
                 //this is the line that actually goes to teh db for the report
                 String record = Report.getTranRecord(reportDate);
     
     
         if (!(record.equals("")))
            {    
               //we need to parse then print
               printReport(parseReport(record));
            }
         else
            {
               lblMessage.setText("There are no reports for that date.");
            }
            }
       else
           {
              lblMessage.setText("You have selected a date that is in the future!");
           }
      }
    catch(Exception e)
    {
       lblMessage.setText("Unable to process report");
    }
     
}//GEN-LAST:event_BtnRunReportMouseClicked

/*
 This method creates a report date
 @param: the string year, month, and date
 */ 
public Date createDate(String year, String month, String day) throws ParseException
	{
		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
		format.setLenient(false);		            
		
     String inputDate = month + "/" + day + "/" + year;
		
		try
		{
			reportDate = format.parse(inputDate);
			
		}
		catch(ParseException e)
		{
         lblMessage.setText("There are no reports for that date.");     
		}	 
		
		return reportDate;		
	}
   /*
    This method actually prints to the screen 
    */ 
public void printReport(String[] array)
{
     
    final String PURHCASE_SALES_LABEL = "Total Purchase Sales:";
    final String NUMBER_ITEMS_PURCHASE_LABEL = "Number of Items Purchased:";
    final String NUMBER_PURCHASE_TRANSACTIONS = "Number of Purchase Transactions:";
    final String RETURN_TOTALS_LABEL = "Total Returns:";
    final String NUMBER_ITEMS_RETURN_LABEL = "Number of Items Returned:";
    final String NUMBER_RETURN_TRANSACTION_LABEL =  "Number of Return Transactions:";
    final String FRAUDUELENT_TOTAL_LABEL = "Number of Fradulent Returns Detected:";
        
    String purchaseSales = String.format("%26s ","$ " +  array[0] + "\n");
    String numberItemsPurchased = String.format("%21s ", array[1] + "\n");
    String numberOfPurchaseTranactions = String.format("%15s ", array[2] + "\n");
    String returnTotals = String.format("%33s ","$ " + array[3] + "\n");
    String numberOfReturnItems = String.format("%22s ", array[4] + "\n");
    String numberofReturnTranactions = String.format("%17s ", array[5] + "\n");
    String fraudulentTotals = String.format("%10s ", array[6] + "\n");
           
    TAReport.append(PURHCASE_SALES_LABEL);
    TAReport.append(purchaseSales + "\n");
    TAReport.append(NUMBER_ITEMS_PURCHASE_LABEL);
    TAReport.append(numberItemsPurchased + "\n");
    TAReport.append(NUMBER_PURCHASE_TRANSACTIONS);
    TAReport.append(numberOfPurchaseTranactions + "\n");
    TAReport.append(RETURN_TOTALS_LABEL);
    TAReport.append(returnTotals + "\n");
    TAReport.append(NUMBER_ITEMS_RETURN_LABEL);
    TAReport.append(numberOfReturnItems + "\n");
    TAReport.append(NUMBER_RETURN_TRANSACTION_LABEL);
    TAReport.append(numberofReturnTranactions + "\n");
    TAReport.append(FRAUDUELENT_TOTAL_LABEL);
    TAReport.append(fraudulentTotals + "\n");     
}
/*
    the record looks like this "500000<>200<>50<>500.00<>20<>10<>2";
    first = total sales
    second = num of Item purhcased
    third = num of purchase transactions
    fourth = total returns
    fifth = num of items returned
    six = num of return transactions
    seven = num of fraudulent returns
  */
public String[] parseReport(String record)
{        
    int i = 0;
    
    for (i = 0; i < reportArray.length - 1; i++)
    {
      String temp = record.substring(0,record.indexOf("<"));
      record = record.substring(record.indexOf("<") + 2, record.length());
      reportArray[i] = temp;
    }     
      //catch the last part
      reportArray[i] = record;     
    return reportArray;
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnRunReport;
    private java.awt.Checkbox CbxToday;
    private javax.swing.JComboBox ComboxDay;
    private javax.swing.JComboBox ComboxMonth;
    private javax.swing.JComboBox ComboxYear;
    private javax.swing.JTextArea TAReport;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDay;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JLabel lblMonth;
    private javax.swing.JLabel lblOK;
    private javax.swing.JLabel lblSelectDate;
    private javax.swing.JLabel lblYear;
    // End of variables declaration//GEN-END:variables

private String[] reportArray = new String[7]; 
private Date reportDate;
}
